﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_bug_DeleteBug : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrbtn.Text = "hide";
            ltrBugSummary.Text = "hide";
            ltrViewDetails.Text = "hide";
            ltrdetails.Text = "hide";
            var projects = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == Master.Master.CurrentUserManagement.CurrentUser.Username)
                           select item.Project;

            foreach (Project item in projects)
            {
                ListItem projectItem = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(projectItem);
            }

            // adding items for type of bugs
            foreach (var item in EnumUtility.EnumToList<BugManagement.Type>())
            {
                DDLTypeOfBug.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Severity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Severity>())
            {
                dllSeverity.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Priority
            foreach (var item in EnumUtility.EnumToList<BugManagement.Priority>())
            {
                dllPriority.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for reproducabillity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Reproducability>())
            {
                dllReproducability.Items.Add(EnumUtility.GetEnumNiceName(item));
            }
        }
    }

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        Logging.ClearLogBug(Convert.ToInt32(Session["Bugid"].ToString()));
        BugManagement objBugManagement = new BugManagement(Master.Master.CurrentUserManagement.CurrentUser);
        bool result = objBugManagement.DeleteBug(Convert.ToInt32(Session["Bugid"].ToString()));
        if (result == true)
        {
            ltrmess.Text = "Delete successfully !!!";
        }
        else
        {
            ltrmess.Text = "Fail !!!";
        }
        ltrdetails.Text = "hide";
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ltrViewDetails.Text = "";
        ddlBugName.Items.Clear();
        var Bugs = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.ProjectId == Convert.ToInt32(ddlProjectName.SelectedValue))
                   select new
                   {
                       item.Summary,
                       item.BugId
                   };

        foreach (var i in Bugs)
        {
            ddlBugName.Items.Add(i.Summary);
        }
        ltrBugSummary.Text = "";
        ltrbtn.Text = "";
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        BugManagement objBugManagement = new BugManagement(Master.Master.CurrentUserManagement.CurrentUser);
        Bug bugrecord = BugManagement.GetBug(Convert.ToInt32(Session["Bugid"].ToString()));

        txtDescription.Text = bugrecord.Description;
        txtFoundInComponent.Text = bugrecord.BugFoundInComponent;
        txtInVersion.Text = bugrecord.BugFoundInVersion;
        dllPriority.SelectedIndex = bugrecord.Priority;
        txtReproductionSteps.Text = bugrecord.ReproductionSteps;

        dllSeverity.SelectedIndex = bugrecord.Severity;
        textSummary.Text = bugrecord.Summary;
        DDLTypeOfBug.SelectedIndex = bugrecord.Type;
        ltrdetails.Text = "";
        ltrbtn.Text = "";
    }

    protected void ddlBugName_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Bugid = (from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.Summary == ddlBugName.SelectedValue)
                     select item.BugId).Single();

        Session["Bugid"] = Bugid;
        ltrViewDetails.Text = "";
        ltrdetails.Text = "hide";
    }
}